<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人信息管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <el-form
        :model="personForm"
        :rules="personFormRules"
        ref="personFormRef"
        label-width="100px"
        class="demo-personForm"
      >
        <el-form-item label="编号" prop="no">
          <el-input v-model="personForm.no" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="personForm.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="personForm.school" disabled></el-input>
        </el-form-item>
        <el-form-item label="学院" prop="college">
          <el-input v-model="personForm.college" disabled></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-input v-model="personForm.sex" disabled></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="changePwdDialogVisible = true"
            >修改密码</el-button
          >
          <el-button type="success" @click="editUserDialogVisible = true"
            >更新个人信息</el-button
          >
          <el-button
            type="danger"
            @click="removePersonDialogVisible = true"
            v-auth="['person:remove']"
            >注销账号</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 修改密码对话框 -->
    <el-dialog
      title="修改密码"
      :visible.sync="changePwdDialogVisible"
      width="50%"
      center
      @close="changePwdDialogClosed"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="passwordForm"
        :rules="passwordFormRules"
        ref="passwordFormRef"
        label-width="100px"
      >
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input v-model="passwordForm.oldPassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="passwordForm.newPassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPwd">
          <el-input v-model="passwordForm.checkPwd" show-password></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="changePwdDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="changePassword">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 注销用户对话框 -->
    <el-dialog
      title="注销用户"
      :visible.sync="removePersonDialogVisible"
      width="30%"
    >
      <span>确定要注销掉该账号吗？</span>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="removePersonDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="deletePerson">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改个人信息对话框 -->
    <el-dialog
      title="修改个人信息"
      :visible.sync="editUserDialogVisible"
      width="50%"
      center
      @close="editUserDialogClosed"
    >
      <!-- 内容主体区 -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="100px"
      >
        <el-form-item label="编号" prop="no">
          <el-input v-model="editForm.no"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-select v-model="editForm.school" @focus="getSchoolList">
            <template v-for="school in schoolList">
              <el-option
                :label="school.name"
                :value="school.id"
                :key="school.id"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="学院" prop="college">
          <el-select v-model="editForm.college" @focus="getCollegeList">
            <template v-for="college in collegeList">
              <el-option
                :label="college.name"
                :value="college.id"
                :key="college.id"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="editForm.sex" @focus="getSexList">
            <template v-for="sex in sexList">
              <el-option
                :label="sex.info"
                :value="sex.code"
                :key="sex.code"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editUserDialogVisible = false">关 闭</el-button>
        <el-button type="primary" @click="updatePerson">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  removePerson,
  updatePerson,
  changePwd,
  getPerson,
} from "@/api/person.js";
import { pageSchool, pageCollege } from "@/api/organizationalmanagement.js";
import { getDict } from "@/api/datadictionary.js";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.passwordForm.checkPwd !== "") {
          this.$refs.passwordFormRef.validateField("checkPwd");
        }
        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.passwordForm.newPassword) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };

    return {
      personForm: {
        no: "", //用户的唯一标识
        username: "", //用户名
        school: "", //学校
        college: "", //学院
        sex: "", //代表的用户的身份
      },
      personFormRules: {
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
      },
      //修改密码的表单
      passwordForm: {
        newPassword: "",
        oldPassword: "",
        checkPwd: "",
      },
      //修改密码的规则
      passwordFormRules: {
        newPassword: [
          {
            required: true,
            trigger: "blur",
            validator: validatePass,
          },
        ],
        oldPassword: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
        ],
        checkPwd: [
          {
            required: true,
            trigger: "blur",
            validator: validatePass2,
          },
        ],
      },
      //修改密码对话框的显示与隐藏
      changePwdDialogVisible: false,
      //注销用户对话框的显示与隐藏
      removePersonDialogVisible: false,
      //学校列表
      schoolList: [],
      //学院列表
      collegeList: [],
      //性别列表
      sexList: [],
      //查询学校标记
      flagSchool: 0,
      //查询学院标记
      flagCollege: 0,
      //性别查询标记
      flagSex: 0,
      //编辑个人信息
      editForm: {
        no: null,
        name: null,
        school: null,
        college: null,
        sex: null,
      },
      editFormRules: {
        // school: [{ required: true, message: "请选择学校", trigger: "blur" }],
        // sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
      },
      //编辑个人信息弹窗
      editUserDialogVisible: false,
    };
  },
  created() {
    this.getSexList();
    this.getPerson();
    this.getSchoolList();
  },
  methods: {
    //获取学校列表
    async getSchoolList() {
      const { data: res } = await pageSchool(1, 300);
      this.schoolList = res.data.records;
    },
    //添加获取学校对应的学院列表
    async getCollegeList() {
      var id = this.editForm.school;
      const { data: res } = await pageCollege(1, 100, id);
      this.collegeList = res.data.records;
    },
    //获取性别
    async getSexList() {
      const { data: res } = await getDict("sex");
      this.sexList = res.data;
    },
    //获得自己的个人信息
    async getPerson() {
      const { data: res } = await getPerson();
      this.personForm = res.data;
      this.sexList.forEach((sex) => {
        if (sex.code === this.personForm.sex) {
          this.personForm.sex = sex.info;
        }
      });
    },
    //更新窗口重置
    editUserDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //更新个人信息
    updatePerson() {
      var organId = null;

      if (this.editForm.college != null) {
        organId = this.editForm.college;
      } else if (this.editForm.school != null) {
        organId = this.editForm.school;
      } else {
        organId = null;
      }

      var personInfo = {
        name: this.editForm.name,
        no: this.editForm.no,
        organId: organId,
        sex: this.editForm.sex,
      };

      updatePerson(personInfo)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("该用户名已经被使用");
          }

          this.editUserDialogVisible = false;
          this.collegeList = [];
          this.getPerson();
        })
        .catch(() => {
          this.$message.error("重复更新");
        });
    },

    //重置修改密码
    changePwdDialogClosed() {
      this.$refs.passwordFormRef.resetFields();
    },
    //确认提交修改密码的表单
    changePassword() {
      if (this.passwordForm.newPassword != this.passwordForm.checkPwd) {
        return this.$message.error("两次密码不同，请再次输入");
      }
      var ChangePassword = {
        oldPassword: this.passwordForm.oldPassword,
        newPassword: this.passwordForm.newPassword,
      };
      changePwd(ChangePassword)
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("旧密码是错误的，请认真填写喔！");
          }
          this.changePwdDialogVisible = false;
          this.getPerson();
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },

    //注销个人账户
    deletePerson() {
      removePerson()
        .then((res) => {
          if (res.data.code !== 0) {
            return this.$message.error("注销失败");
          }
          this.$router.push("/Login");
        })
        .catch(() => {
          this.$message.error("请求出错");
        });
    },
  },
};
</script>

<style lang="less" scoped>
.person_form {
  margin-left: 15px;
  margin-right: 15px;
  margin-top: 5px;
}
.person_container {
  background-color: grey;
  height: 100%;
}
.person_box {
  width: 450px;
  height: 660px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .btns {
    display: flex;
    justify-content: space-around;
  }
}
.box-card {
  margin-top: 20px !important;
}
</style>